package com.teamwork.module03.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.common.UserIcon;
import com.teamwork.common.entity.module03.Article;
import com.teamwork.common.entity.module03.Author;
import com.teamwork.common.result.Result;
import com.teamwork.module03.mapper.AuthorMapper;
import com.teamwork.module03.service.ArticleService;
import com.teamwork.module03.service.AuthorService;
import com.teamwork.module03.service.UserIconService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
@Slf4j
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author> implements AuthorService {

    @Resource
    @Lazy
    private ArticleService articleService;
    @Resource
    private UserIconService userIconService;

    //根据id查询作者
    @Override
    public String getUserNameById(Long authorId) {
        LambdaQueryWrapper<Author> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Author::getId,authorId);
        return this.getById(authorId).getUsername();
    }

    @Override
    public String getNickNameById(Long authorId) {
        LambdaQueryWrapper<Author> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Author::getId,authorId);
        return this.getById(authorId).getNickname();
    }

    /**
     * 根据用户id查询作者
     * @param userid 用户id
     * @return Author对象
     */
    @Override
    public Author getByUserId(Integer userid) {
        LambdaQueryWrapper<Author> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Author::getUserId,userid);
        return this.getOne(wrapper);
    }

    /**
     * 根据用户id删除作者
     * @param id 用户id
     * @return 是否删除
     */
    @Override
    public boolean deleteByUserId(Integer id) {
        LambdaQueryWrapper<Author> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Author::getUserId,id);
        boolean b = this.remove(wrapper);
        if(b){
            log.info("根据用户id：{} 删除一条作者记录",id);
        }//else log.error("根据用户id：{} 删除一条作者记录出错！！！",id);
        return b;
    }

    /**
     * 注册时初始化作者表
     * @param userid  用户id
     * @param username  用户名
     * @param nickname 昵称
     * @return 是否新增成功
     */
    @Override
    public boolean initOne(Long userid, String username, String nickname) {

        Author author =new Author();
        author.setUserId(userid);
        author.setUsername(username);
        author.setNickname(nickname);
        author.setCares(0);
        author.setArticles(0);


        return this.save(author);
    }

    /**
     * 根据文章id获取作者的信息
     * @param artid 文章id
     * @return
     */
    @Override
    public Result getByArtId(Integer artid) {

        Article article = articleService.getById(artid);
        if(article==null){
            return Result.failure("该文章id不存在！");
        }
        Long authorId = article.getAuthorId();

        Author author = this.getById(authorId);

        String nickname = author.getNickname();

        //再查询作者的头像
        Long userId = author.getUserId();
        String icon = userIconService.getByUserId(Math.toIntExact(userId)).getIcon();

        HashMap<String, Object> map = new HashMap<>();
        map.put("authorid",author.getId());
        map.put("nickname",nickname);
        map.put("authoricon",icon);
        return Result.success(map);

    }
}
